# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=trunk
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=0.21.14
pkgrel=1
pkgdesc='Build, bundle & ship your Rust WASM application to the web (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://trunkrs.dev/'
msys2_repository_url='https://github.com/trunk-rs/trunk'
msys2_references=(
  'purl: pkg:cargo/trunk'
)
license=('spdx:MIT OR Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-rust-wasm")
makedepends=("${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-mdbook-admonish"
             "${MINGW_PACKAGE_PREFIX}-rust")
source=("$msys2_repository_url/archive/v$pkgver/$_realname-$pkgver.tar.gz")
sha256sums=('8687bcf96bdc4decee88458745bbb760ad31dfd109e955cf455c2b64caeeae2f')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd "${srcdir}"
  rm -rf "build-${MSYSTEM}" | true
  cp -r "${_realname}-${pkgver}" "build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"

  "${MINGW_PREFIX}/bin/cargo.exe" build \
    --no-default-features --features native-tls \
    --release \
    --frozen

  msg "Build documentation"
  cd "${srcdir}/build-${MSYSTEM}/guide"
  "${MINGW_PREFIX}/bin/mdbook-admonish.exe" install .
  "${MINGW_PREFIX}/bin/mdbook.exe" build
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  "${MINGW_PREFIX}/bin/cargo.exe" test \
    --no-default-features --features native-tls \
    --release \
    --frozen
}

package_trunk() {
  cd "${srcdir}/build-${MSYSTEM}"

  install -Dm755 "target/release/${_realname}.exe" "${pkgdir}${MINGW_PREFIX}/bin/${_realname}.exe"
  install -Dm644 "LICENSE-MIT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-MIT"
  install -Dm644 "LICENSE-APACHE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE-APACHE"
}

package_trunk-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
  cp -a "${srcdir}/build-${MSYSTEM}/guide/book/html" "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/html"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;

# vim:set ts=2 sw=2 et:
